package org.jeecg.modules.rule.service.impl;

import org.jeecg.modules.rule.entity.Autossc;
import org.jeecg.modules.rule.entity.AutosscDetail;
import org.jeecg.modules.rule.mapper.AutosscDetailMapper;
import org.jeecg.modules.rule.mapper.AutosscMapper;
import org.jeecg.modules.rule.service.IAutosscService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 自动脱敏订阅表
 * @Author: jeecg-boot
 * @Date:   2019-05-24
 * @Version: V1.0
 */
@Service
public class AutosscServiceImpl extends ServiceImpl<AutosscMapper, Autossc> implements IAutosscService {

	@Autowired
	private AutosscMapper autosscMapper;
	@Autowired
	private AutosscDetailMapper autosscDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(Autossc autossc, List<AutosscDetail> autosscDetailList) {
		autosscMapper.insert(autossc);
		for(AutosscDetail entity:autosscDetailList) {
			//外键设置
			entity.setFdSscid(autossc.getId());
			autosscDetailMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(Autossc autossc,List<AutosscDetail> autosscDetailList) {
		autosscMapper.updateById(autossc);
		
		//1.先删除子表数据
		autosscDetailMapper.deleteByMainId(autossc.getId());
		
		//2.子表数据重新插入
		for(AutosscDetail entity:autosscDetailList) {
			//外键设置
			entity.setFdSscid(autossc.getId());
			autosscDetailMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		autosscDetailMapper.deleteByMainId(id);
		autosscMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			autosscDetailMapper.deleteByMainId(id.toString());
			autosscMapper.deleteById(id);
		}
	}
	
}
